package lee.vioson.ebook.ui.components

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import lee.vioson.ebook.R
import lee.vioson.ebook.data.AppContainer
import lee.vioson.ebook.ui.AppTheme
import lee.vioson.ebook.viewmodels.BookDetailViewModel
import lee.vioson.remotedata.bean.Book

/**
 *Author:viosonlee
 *Date:2023/6/14
 *DESCRIPTION:
 */
@Composable
fun BookDetail(
    navHostController: NavHostController,
    appContainer: AppContainer,
    bookId: String,
    viewModel: BookDetailViewModel = viewModel(factory = viewModelFactory {
        initializer { BookDetailViewModel(appContainer, bookId) }
    })
) {
    val state by viewModel.state.collectAsState()
    val bookInfo by viewModel.bookInfo.collectAsState()

    LaunchedEffect(key1 = Unit, block = {
        viewModel.markView()
    })

    Scaffold(topBar = {
        TopBar(
            modifier = Modifier
                .fillMaxWidth()
                .background(AppTheme.colors.background),
            navHostController = navHostController,
            action = {
                Loading(
                    Modifier
                        .fillMaxSize()
                        .clickable {
                            //collection action
                            viewModel.addCollection(!state.isCollection)
                        },
                    showLoading = state.collectionLoading
                ) {
                    Image(
                        painter = painterResource(
                            id = if (state.isCollection) R.mipmap.ic_collected
                            else R.mipmap.ic_add_collect
                        ),
                        contentDescription = "collection button",
                        modifier = Modifier.size(24.dp)
                    )
                }
            }
        )
    }) { paddingValues ->
        NextScrollableBox(
            modifier = Modifier.padding(paddingValues),
            header = {
                bookInfo?.bookInfo?.let { BookDetailHeader(it, navHostController) }
            },
            listItem = {
                bookInfo?.chapterList?.let { chapters ->
                    itemsIndexed(chapters) { _, chapter ->
                        ChapterItem(
                            chapter = chapter,
                            book = bookInfo,
                            navHostController
                        )
                        Divider()
                    }
                }
            })
    }

}

@Composable
fun BookDetailHeader(book: Book, navHostController: NavHostController) {
    AsyncImage(
        model = book.coverPhoto,
        contentDescription = "coverPhoto",
        modifier = Modifier
            .fillMaxWidth()
            .height(500.dp),
        contentScale = ContentScale.FillWidth
    )
    Column(Modifier.fillMaxSize()) {
        VipGuide(
            modifier = Modifier
                .padding(vertical = 16.dp, horizontal = 8.dp)
                .fillMaxWidth()
                .height(70.dp)
                .background(Color.Black.copy(alpha = 0.3f)),
            navHostController = navHostController
        )
        Spacer(modifier = Modifier.weight(1f))
        Column(
            Modifier
                .fillMaxWidth()
                .background(Color.Black.copy(alpha = 0.5f))
                .padding(8.dp)
        ) {
            Text(
                text = book.bookName,
                fontSize = 16.sp,
                color = AppTheme.colors.onBackground
            )
            Spacer(modifier = Modifier.height(3.dp))
//                            Text(
//                                text = book.typeString,
//                                fontSize = 14.sp,
//                                color = AppTheme.colors.onBackground
//                            )
//                            Spacer(modifier = Modifier.height(8.dp))
            Row(Modifier.fillMaxWidth()) {
//                                book.marks.forEach {
//                                    BookMark(content = it, color = Color.Red)
//                                    Spacer(modifier = Modifier.width(2.dp))
//                                }
            }
            Spacer(modifier = Modifier.height(8.dp))
            Row(
                Modifier.fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically
            ) {
                RatingBar(starsColor = AppTheme.appColors.ratingColor)
                Icon(
                    painter = painterResource(id = R.mipmap.ic_fire),
                    contentDescription = "fire",
                    tint = AppTheme.appColors.redTextColor,
                    modifier = Modifier.size(13.dp)
                )
                Text(
                    text = book.hotCount.toString(),
                    fontSize = 10.sp,
                    color = AppTheme.appColors.redTextColor
                )
            }
            Text(
                text = book.briefIntroduction,
                fontSize = 14.sp,
                color = AppTheme.appColors.whiteText80
            )
        }
    }
}